package castledefense.gui.upgradePanel;

import twl.global.widget.LabelFonts;

import castledefense.data.BuildingData;
import castledefense.gui.BuildingButton;
import castledefense.logic.Map;
import castledefense.logic.buildings.Tower1;
import castledefense.logic.buildings.Tower2;

import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.renderer.AnimationState.StateKey;

public class BuildingButtonInfoPanel extends Widget {
	
	public static final StateKey STATE_TOWER1 = StateKey.get("tower1");
	public static final StateKey STATE_TOWER2 = StateKey.get("tower2");
	public static final StateKey STATE_GATE = StateKey.get("gate");
	public static final StateKey STATE_DESTROY = StateKey.get("destroy");
	
	private Widget buildingImage;
	private LabelFonts labelTitle;
	private LabelFonts labelHotkey;
	private LabelFonts labelDescription;
	private LabelFonts labelDamage;
	private LabelFonts labelRange;
	private LabelFonts labelCost;
	
	public BuildingButtonInfoPanel() {
		//BuildingButtonPanel
		int width = (int) (Map.cellWidth * 1.5);
		int height = (int) (Map.cellWidth * 1.5);
		width = 45;
		height = 45;

		setTheme("");
		
		buildingImage = new Widget();
		buildingImage.setTheme("buildingIconTheme");
		buildingImage.setSize(width, height);
		buildingImage.setPosition(10, 10);
		add(buildingImage);

		labelTitle = new LabelFonts();
		labelTitle.setTheme("labelTheme");
		labelTitle.setFont("arialBold20");
		labelTitle.setPosition(62, 20);
		add(labelTitle);

		labelHotkey = new LabelFonts();
		labelHotkey.setTheme("labelTheme");
		labelHotkey.setFont("arialItalic16");
		labelHotkey.setColor(LabelFonts.STATE_BLUE);
		labelHotkey.setPosition(62, 42);
		add(labelHotkey);
		
		Widget line = new Widget();
		line.setTheme("fillRectBlack");
		line.setSize(159, 1);
		line.setPosition(10, 60);
		add(line);
		
		labelDescription = new LabelFonts();
		labelDescription.setTheme("labelTheme");
		labelDescription.setFont("arialItalic16");
		labelDescription.setPosition(10, 80);
		add(labelDescription);
		
		LabelFonts damage = new LabelFonts("Damage");
		damage.setTheme("labelTheme");
		damage.setFont("arialBold16");
		damage.setPosition(10, 110);
		add(damage);
		
		LabelFonts range = new LabelFonts("Range");
		range.setTheme("labelTheme");
		range.setFont("arialBold16");
		range.setPosition(10, 125);
		add(range);
		
		LabelFonts cost = new LabelFonts("Cost");
		cost.setTheme("labelTheme");
		cost.setFont("arialBold16");
		cost.setPosition(10, 140);
		add(cost);
		
		labelDamage = new LabelFonts("Damage");
		labelDamage.setTheme("labelTheme");
		labelDamage.setFont("arialBold15");
		labelDamage.setPosition(70, 110);
		add(labelDamage);
		
		labelRange = new LabelFonts("Range");
		labelRange.setTheme("labelTheme");
		labelRange.setFont("arialBold15");
		labelRange.setPosition(70, 125);
		add(labelRange);
		
		labelCost = new LabelFonts("Cost");
		labelCost.setTheme("labelTheme");
		labelCost.setFont("arialBold15");
		labelCost.setPosition(70, 140);
		add(labelCost);
		
	}
	
	public void draw(BuildingButton button, Widget rootPane) {
		labelTitle.setText(button.getText());
		labelHotkey.setText("(Hotkey = " + button.getHotkey() + ")");
		if("Tower1".equals(button.getText())){
			Tower1 tower1 = new Tower1(null, 0, 0, 0);
			labelDescription.setText("A basic tower that attack a\nsingle target.");
			labelDamage.setText(":  " + tower1.getDamage());
			labelRange.setText(":  " + tower1.getRange());
			labelCost.setText(":  " + BuildingData.getCost(BuildingData.tower1) + "$");
		}else if("Tower2".equals(button.getText())){
			Tower2 tower2 = new Tower2(null, 0, 0, 0);
			labelDescription.setText("A short range tower that\nattack every enemy around.");
			labelDamage.setText(":  " + tower2.getDamage());
			labelRange.setText(":  " + tower2.getRange());
			labelCost.setText(":  " + BuildingData.getCost(BuildingData.tower2) + "$");
		}else if("Destroy".equals(button.getText())){
			labelDescription.setText("Sell a building when you\nclick on it.");
			labelDamage.setText(":  --");
			labelRange.setText(":  --");
			labelCost.setText(":  - 75% of cost");
		}
		
		buildingImage.getAnimationState().setAnimationState(STATE_TOWER1, "Tower1".equals(button.getText()));
		buildingImage.getAnimationState().setAnimationState(STATE_TOWER2, "Tower2".equals(button.getText()));
		buildingImage.getAnimationState().setAnimationState(STATE_GATE, "Gate".equals(button.getText()));
		buildingImage.getAnimationState().setAnimationState(STATE_DESTROY, "Destroy".equals(button.getText()));
		
		if(!(rootPane.getNumChildren() == 1 && rootPane.getChild(0) == this)){
			rootPane.removeAllChildren();
			rootPane.add(this);
		}
	}

}
